//
//  OTTool.m
//  OT_OC
//
//  Created by yunmian on 2019/7/16.
//  Copyright © 2019 James. All rights reserved.
//

#import "OTTool.h"
#import "UIView+Toast.h"

@implementation OTTool

+ (UIWindow *)keyWindow {
    return [UIApplication sharedApplication].keyWindow;
}

+ (BOOL)isStringEmptyWithText:(NSString *)text {
    return !(text && [text isKindOfClass:[NSString class]] && text.length > 0);
}

+ (BOOL)isArrayEmptyWithArray:(NSArray *)array {
    return !(array != nil && ![array isKindOfClass:[NSNull class]] && array.count != 0);
}

+ (void)showToastWithText:(NSString *)text {
    [[self keyWindow] makeToast:text duration:1 position:CSToastPositionCenter];
}

+ (NSString *)trimNumStrWithText:(NSString *)text {
    if (![text containsString:@"."]) {
        return text;
    }
    NSInteger dotIndex = [text rangeOfString:@"."].location;
    
    if (text.length - dotIndex < 3) {
        return  text;
    }
    NSString *numStr = [text substringToIndex: dotIndex + 3];
    NSString *lastStr = [numStr componentsSeparatedByString:@"."].lastObject;
    if ([lastStr isEqualToString:@"00"]) {
        return [text substringToIndex: dotIndex];
    }
    return [text substringToIndex: dotIndex + 4];
}
@end
